<?php

require_once 'classes/LightPressPlugin.php';

class SecondPage extends LightPressPlugin {
    
    var $constructor_args = array(
        'date_format'=>'custom date format',
        'limit'=>'nmber of posts to show');
    var $default_context = LP_CONTEXT_INDEX;
    var $description = 'Display the top n most recent posts';
    
    var $active = true;
    var $hooks = array('sidebar');
    
    var $date_format;
    var $limit = 5;
    
    function SecondPage(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        if (!$dummy_run && $frontend->context['index'])
            $this->active = false;
    }
    
    function run($hook, &$hook_data) {
        
        $frontend =& $this->_frontend;
        
        $tpl =& $frontend->tpl;
        $db =& $frontend->db;
        $date_format = (empty($this->date_format) ? $frontend->options['date_format'] : $this->_date_format);
        
        $tpl->setFile('plugin_secondpage', 'plugins/second_page.xml');

        $q = "select id, post_title as title, post_name as name, unix_timestamp(post_date) as tstamp, post_category " .
            "from " . $frontend->tables['posts'] . " p " .
            "where (p.post_status='publish') and p.post_date_gmt <= '" . $frontend->where_date . "' " .
            "order by p.post_date desc limit {$this->_frontend->options['posts_per_page']}, {$this->limit}";

        if (!$db->query($q))
            return $this->hide();
        
        $post = array();
        foreach ($db->all() as $k => $v) {
            $v['permalink'] = $this->_frontend->getPermalink($v['id'], $v['name'], $v['tstamp'], $v['post_category']);
            $v['date'] = strftime($date_format, $v['tstamp']);
            $v['short_date'] = strftime($frontend->options['short_date_format'], $v['tstamp']);
            $posts[] = $v;
        }
        $tpl->parseBlock('PLUGIN_SECONDPAGE_POST', 'plugin_secondpage_post', $posts, 'plugin_secondpage');
        $tpl->parse('PLUGIN_SECONDPAGE', 'plugin_secondpage');
    }
    
}

?>
